<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link href="css/public.css" type="text/css" rel="stylesheet"/>
    <script src="js/jquery-1.7.2.js"></script>
    <script src="js/mapConfig.js"></script>
    <script src="objClass/objQuery.js"></script>
    <script src="objClass/objFunction.js"></script>
    <script src="objClass/objGeoStyle.js"></script>
    <script src="objClass/objBubble.js"></script>
    <script src="objClass/objGeometry.js"></script>
    <script src="objClass/objInfowindow.js"></script>
    <script src="js/mapThemePopo.js"></script>
    <script src="js/mapPublicFunction.js"></script>
    <script src="js/mapShowLocation.js"></script>

    <style>
        .esriPopup .titleButton.maximize {
            display: none;
        }
    </style>
    <script type="text/javascript">
        var receiveObj = {};             //
        var receiveMsgCity = "";         // 从前端接受到的城市名称
        var receiveMsgCommunityId = "";  // 从前端接受到的小区ID
        var receiveMsgMatchStatus = "";  // 匹配状态

        var map;
        var baseLayer;
        var imgLayer;//影像底图
        var showType;//记录当前显示类型（影像底图、矢量底图）

        var hightliteLayer;
        var graphicLayerPoint;                // 点图层，用来存放小区，楼栋
        var cityCode;                         // 标识城市，北京 大连 丹东 珠海
        var graphicLayerPolygonsArray = [];   //记录面要素层存放数据的id.
        var _extent = [];                     //

        var LdCode = "";
        var buildingList=[];

        //加载ArcgisAPI样式
        objFunction.loadCss(config.urlAPI + "/dijit/themes/claro/claro.css");
        objFunction.loadCss(config.urlAPI + "/esri/css/esri.css");
        //加载ArcgisAPI脚本
        objFunction.loadJs(config.urlAPI + "/init.js");

        function initPage() {
            dojo.require("esri.map");
            dojo.require("esri.geometry.geodesicUtils");
            dojo.require("esri.tasks.GeometryService");
            dojo.require("esri.tasks.AreasAndLengthsParameters");
            dojo.require("esri.SpatialReference");
            dojo.require("esri.tasks.query");
            dojo.require("dojo.parser");
            dojo.require("esri.Color");
            dojo.require("esri.symbols.Font");
            dojo.require("esri.symbols.TextSymbol");
            dojo.require("esri.tasks.geometry");
            dojo.require("esri.symbols.SimpleFillSymbol");
            dojo.require("esri.toolbars.draw");
            dojo.require("esri.tasks.IdentifyTask");
            dojo.require("esri.tasks.IdentifyParameters");
            dojo.require("esri.units");

            //初始化地图div
            initMap();
            showType = configShowLayerType.Vector;
            baseLayer = new esri.layers.ArcGISTiledMapServiceLayer(config.urlBaseLayer);
            //添加矢量图到map
            map.addLayer(baseLayer);

            imgLayer = new esri.layers.ArcGISTiledMapServiceLayer(config.urlImgLayer);
            map.addLayer(imgLayer);
            imgLayer.hide();

            //初始化基类
            initObj();
            //向地图添加临时层
            addTempLayer();
            //定位到指定范围
//            setExtent("3101");
            // 绑定地图事件
            BindEvent();

        }
        function initObj() {
            objQuery.init(config.wkid, map);
            objGeometry.init(config.wkid);
            objInfowindow.init(map);
        }

        //初始化地图div
        function initMap() {

            map = new esri.Map(
                'map'
                , {
                    logo: false,
                    slider: true,
                    minZoom: config.minZoom,
                    sliderStyle: "large"
                });
        }

        function setExtent(e) {
            //定位到指定范围
           var extent = new esri.geometry.Extent({
                "xmin": config.xmin_DL, "ymin": config.ymin_DL, "xmax": config.xmax_DL,
                "ymax": config.ymax_DL, "spatialReference": {
                  "wkid": config.wkid
                }
              });

            if (e == "北京市") { //北京
                extent = new esri.geometry.Extent({
                    "xmin": config.xmin_BJ, "ymin": config.ymin_BJ, "xmax": config.xmax_BJ,
                    "ymax": config.ymax_BJ, "spatialReference": {
                        "wkid": config.wkid
                    }
                });
            }
            if (e == "丹东市") { // 丹东
                extent = new esri.geometry.Extent({
                    "xmin": config.xmin_DD, "ymin": config.ymin_DD, "xmax": config.xmax_DD,
                    "ymax": config.ymax_DD, "spatialReference": {
                        "wkid": config.wkid
                    }
                });
            }
            if (e == "珠海市") { //珠海
                extent = new esri.geometry.Extent({
                    "xmin": config.xmin_ZH, "ymin": config.ymin_ZH, "xmax": config.xmax_ZH,
                    "ymax": config.ymax_ZH, "spatialReference": {
                        "wkid": config.wkid
                    }
                });
            }
            if (e == "大连市" || e == "" || e == null) { // 大连
                extent = new esri.geometry.Extent({
                    "xmin": config.xmin_DL, "ymin": config.ymin_DL, "xmax": config.xmax_DL,
                    "ymax": config.ymax_DL, "spatialReference": {
                        "wkid": config.wkid
                    }
                });
            }
            //map.setExtent(extent);
            map.centerAndZoom(extent.getCenter(), 10);

//	        var msg = {};
//	        msg.type = "xq";
//	        msg.cityCode = "3101";
//	        msg.id = "84185797";
//	        msg.x = "1.3528051990000002E7";
//	        msg.y = "4701321.7826000005";
//	        sendMessage(msg);
        }

        //矢量影像切换方法
        function reloadMap(type) {
            if (type == showType) return;

            if (showType == configShowLayerType.Img) {
                //basemap.show();
                showType = configShowLayerType.Vector;
            }
            else if (showType == configShowLayerType.Vector) {
                //imgLayer.show();
                showType = configShowLayerType.Img;
            }
            else {
                return;
            }

            if (showType == configShowLayerType.Img) {
                imgLayer.show();
                baseLayer.hide();
            }
            if (showType == configShowLayerType.Vector) {
                baseLayer.show();
                imgLayer.hide();
            }
        }


        //添加临时层
        function addTempLayer() {
            hightliteLayer = new esri.layers.GraphicsLayer();
            map.addLayer(hightliteLayer);

            graphicLayerPoint = new esri.layers.GraphicsLayer();
            map.addLayer(graphicLayerPoint);
        }
        //地图事件绑定
        function BindEvent() {
//            dojo.connect(map, "onUpdateEnd", function (e, l, le, lod) {
//                ExtentChanged();
//            });
            dojo.connect(graphicLayerPoint, "onClick", function (e) {
                var content = "";
                content += "楼栋名称：" + e.graphic.attributes.LABEL + "<br/>";
                content += "楼栋地址：" + e.graphic.attributes.ADDR_FULL_NAME + "<br/>";
                showInfoWindows(map, e.graphic.geometry, "楼栋信息", content, 300, 200);

              var msg = {};
              msg.buildingId = e.graphic.attributes.ADDRESS_ID;
              msg.buildingCode = '';
              if(buildingList.length>0){
                foo: for(var i=0,len=buildingList.length;i<len;i++){
                  if(buildingList[i].buildingId==msg.buildingId){
                    msg.buildingCode = buildingList[i].buildingCode;
                    break foo;
                  };
                }
              }
              sendMessage(msg);
            });
        }

        function ExtentChanged() {
            /*dojo.connect(map, "onZoomEnd", function (extent, zoomFactor, pt, level) {
                //graphicLayerPolygon.clear();
            });*/

        	  if (map.getLevel() <= 7) {
                showNationalPolygons();
            }
            if (map.getLevel() >= 8 && map.getLevel() <= 10) {
              showProvincePolygon();
              graphicLayerPolygon.show();
            }
        	  if (map.getLevel() >= 11 && map.getLevel() <= 14) {
                showDistrictPolygon();
                graphicLayerPolygon.show();
            }
        	  if (map.getLevel() >= 15) {
                graphicLayerPolygon.hide();
                var selCity = $("#jumpCity").val();
                if (selCity == "1101")
                  showCommunity(config.cityData_BJ + config.urlCommunity);
                else if (selCity == "3101")
                  showCommunity(config.cityData_DL + config.urlCommunity);
                else if (selCity == "4401")
                  showCommunity(config.cityData_DD + config.urlCommunity);
                else if (selCity == "4403")
                  showCommunity(config.cityData_ZH + config.urlCommunity);
            }
        }

        function jumpCity() {
            setExtent($("#jumpCity").val());
        }

        // 清除地图和数组
        function clearGraphic() {
          graphicLayerPointsArray = [];
          graphicLayerLablesArray = [];
        }

        function bindEvent(element, eventName, eventHandler) {
	        if (element.addEventListener) {
		        element.addEventListener(eventName, eventHandler, false);
	        } else if (element.attachEvent) {
		        element.attachEvent('on' + eventName, eventHandler);
	        }
        }

        bindEvent(window, 'message', function (e) {
          var obj = JSON.parse(e.data);
          var city = obj.city;
          var list = obj.buildingList;
          buildingList = obj.buildingList;

          setExtent(city);       // 定位到指定城市

          for (var i = 0; i < list.length; i++) {
          	var buildingId = list[i].buildingId;
          	LdCode = list[i].buildingCode;

            queryBuildingByAddressID(city, buildingId);
          }
        });

        //向父级发送消息
        var sendMessage = function (msg) {
          window.parent.postMessage(msg, '*');
        };

    </script>
</head>
<body style="height:100%; width:100%" onload="initPage()" class="tundra">
<div id="map" style="position:absolute; left:1%; top:1%; width:99%; height:99%;">
</div>
<!--<div style="position:absolute; right:1%; top:1%;">-->
    <!--<label>切换城市：</label>-->
    <!--<select name="jumpCity" id="jumpCity" onchange="jumpCity();">-->
        <!--<option value="1101">北京市</option>-->
        <!--<option value="3101">大连市</option>-->
        <!--<option value="4401">丹东市</option>-->
        <!--<option value="4403">珠海市</option>-->
    <!--</select>-->
<!--</div>-->

</body>
</html>



